package com.example.springblog.model;

import com.example.springblog.enums.ResultStatus;
import lombok.Data;

@Data
public class Result<T> {
    private ResultStatus code;
    private String errMsg;
    private T data;

    //统一结果返回类型

    /**
     * 访问成功返回的类型
     * @param data
     * @return
     * @param <T>
     */
    public static<T> Result<T> success(T data){
        Result result=new Result();
        result.setCode(ResultStatus.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 访问失败的情况
     * @param errMsg
     * @return
     * @param <T>
     */
    public static<T> Result<T> fail(String errMsg){
        Result result=new Result();
        result.setCode(ResultStatus.FAIL);
        result.setData(null);
        result.setErrMsg(errMsg);
        return result;
    }

    public static<T> Result<T> fail(String errMsg, T data){
        Result result=new Result();
        result.setCode(ResultStatus.FAIL);
        result.setData(data);
        result.setErrMsg(errMsg);
        return result;
    }

}
